<?php





namespace app\api\service;





use app\api\model\SubjectCourse;

use app\api\model\SubjectCourseError;

use app\api\model\SubjectCourseLog;

use app\api\model\SubjectType;

use app\common\lib\Redis;

use app\lib\exception\SubjectExcption;



class Subject

{



    /**

     * 获取分类列表

     * @param $course

     * @return false|\PDOStatement|string|\think\Collection

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function getTypeList($course,$car_status){

        if ($car_status == 1){//小车

            $condition = [1];//小车分类

            $result = SubjectType::getTypeList($course,$condition);

        }elseif($car_status == 2){//货车

            $condition = [1,2];

            $result = SubjectType::getTypeList($course,$condition);

        }elseif ($car_status == 3){//客车

            $condition = [1,3];

            $result = SubjectType::getTypeList($course,$condition);

        }

        return $result;

    }





    /***

     * @param $uid

     * @param $course

     * @param $car_status

     * @return array|false|\PDOStatement|string|\think\Collection

     * @throws SubjectExcption

     * @throws \think\Exception

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function SubjectExercises($uid,$course,$car_status)

    {

        $LogModel = new SubjectCourseLog();

        //科目一/四查询是否做过题没，如果没有做过就从头开始;

        $Subject = SubjectCourseLog::get(function ($query)use ($uid,$course,$car_status){

            $query->where('user_id',$uid)->where('course',$course)->where('car_status',$car_status);

        });

        $MentModel = new SubjectCourseLog();

        //错题的数量

        $StatusWrong = 1;

        $WrongNum = $MentModel->SubjectDecide($uid,$course,$StatusWrong,$car_status);

        //对题的数量

        $StatusRight = 2;

        $RightStatus = $MentModel->SubjectDecide($uid,$course,$StatusRight,$car_status);
        //获取做题记录
        $EndSubject = Redis::getInstance()->get(28,'course:tag:uid:'.$uid.':course:'.$course.':car_status:'.$car_status);
        if (empty($EndSubject)){

            $EndSubject = 0;

        }
        if (empty($Subject)){

            //如果没有做题就先头开始做，顺便记录log表
            $LogModel->SubjectLog($uid,$course,$car_status);

            $res = $LogModel->SubjectList($uid,$course,$car_status);

            $NewMsg = [$WrongNum,$RightStatus,$EndSubject,$res];

            return $NewMsg;

       }else{

           //判断用户做完题目没有:

           $LastCourse = SubjectCourseLog::get(function ($query)use ($uid,$course,$car_status){

               $query->where('user_id',$uid)->where('course',$course)->where('status','=',0)->where('car_status',$car_status)

                   ->field(['course_id']);

           });

           if (empty($LastCourse)){

               throw new SubjectExcption([

                   'msg'=>'您已经做完科目'.$course.'的所有题了',

                   'error_code'=>80000

               ]);

           }else{

               $res = $LogModel->SubjectList($uid,$course,$car_status);

               $NewMsg = [$WrongNum,$RightStatus,$EndSubject,$res];

               return $NewMsg;

           }

       }

    }





    /*** 对错判断

     * @param $uid

     * @param $course_id

     * @param $status

     * @param $choice

     * @param $car_status

     * @param $course

     * @param $num

     * @return bool|false|int|string

     * @throws \think\Exception

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function SubjectWhether($uid,$course_id,$status,$choice,$car_status,$course,$num){
        //存入做题记录
            Redis::getInstance()->del(28,'course:tag:uid:'.$uid.':course:'.$course.':car_status:'.$car_status);
            Redis::getInstance()->set(28,'course:tag:uid:'.$uid.':course:'.$course.':car_status:'.$car_status,json_encode($num),0);
        $model = new SubjectCourseLog();

        $res = $model->SubjectWhether($uid,$course_id,$status,$choice,$car_status,$course);

        $NewStatus = $status*1;

        //错题进入错题Error表:

        if ( $NewStatus ===2 ){

            $c_id = $model->where('course_id',$course_id)->where('car_status',$car_status)->where('user_id',$uid)->find()->toArray();

            $Emodel = new SubjectCourseError();

            $cou_id = $c_id['course'];

            $Cid=SubjectCourseError::get(function ($query)use ($uid,$course_id,$car_status){

                $query->where(['user_id'=>$uid,'course_id'=>$course_id,'car_status'=>$car_status]);

            });

            //判断错题表中是否存在当前题目:

            if (empty($Cid)){

                $Emodel->InsertSubject($cou_id,$uid,$course_id,$car_status);

            }

        }

        return $res;

    }



    /*** 查询用户错题

     * @param $uid

     * @return false|\PDOStatement|string|\think\Collection

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function SubjectWrongExercises($uid){

        $model = new SubjectCourseError();

        $result = $model->WrongExercises($uid);

        return $result;

    }





    /*** 分类练习

     * @param $course

     * @param $car_status

     * @return false|\PDOStatement|string|\think\Collection

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function SubjectSort($id,$course,$car_status){

        $Model = new SubjectType();

        $Ids = $id*1;

        $result = $Model->SubjectCourseList($Ids,$course,$car_status);

        return $result;

    }





    /*** 随机练习

     * @param $uid

     * @param $course

     * @return array

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function SubjectRandom($uid,$course)

    {

        $Num = Redis::getInstance()->get(26,'subject:number'.$uid);

        if (empty($Num)){

            $info = Redis::getInstance()->get(25,'subject:random:course'.$uid);

            $res = [$info,$Num];

            if (!empty($info)){

                return $res;

            }else{

                $Model = new SubjectCourse();

                $data = $Model->SubjectRandom($course);

                $info = $data->shuffle();

                Redis::getInstance()->set(25,'subject:random:course'.$course,json_encode($info->toArray()),48*3600);

                $res = [$info,$Num];

                return $res;

            }

        }else{

            $info = Redis::getInstance()->get(25,'subject:random:course'.$uid);

            $res = [$info,$Num];

            if (!empty($info)){

                return $res;

            }else{

                $Model = new SubjectCourse();

                $data = $Model->SubjectRandom($course);

                $info = $data->shuffle();

                Redis::getInstance()->set(25,'subject:random:course'.$course,json_encode($info->toArray()),48*3600);

                $res = [$info,$Num];

                return $res;

            }

        }



    }



    /*** 上次随机练习的题目

     * @param $uid

     * @param $number

     * @return bool|string

     */

    public function SubjectNumber($uid,$number){

        $num = Redis::getInstance()->get(26,'subject:number'.$uid);

        if (!empty($num)){

            if (isset($num)){

                Redis::getInstance()->del(26,'subject:number'.$uid);

                Redis::getInstance()->set(26,'subject:number'.$uid,json_encode($number),48*3600);

                return $num;

            }

            return $num;

        }else{

           $num =  Redis::getInstance()->set(26,'subject:number'.$uid,json_encode($number),48*3600);

           return $num;

        }

    }



}

